<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>增加修改页面</title>
    <link rel="stylesheet" href="../../res/layui/css/layui.css" media="all">
    <script type="text/javascript" src="../../res/js/jquery-3.1.1.min.js"></script>
    <script type="text/javascript" src="../../res/layui/layui.js"></script>
    <script type="text/javascript" src="../../res/js/tools.js"></script>
    <style type="text/css">
        #dicFrom{
            margin: 40px;
        }
    </style>
</head>
<body>
<form class="layui-form" action="" lay-filter="example" id="dicFrom">
    <input type="hidden" name="did" id="did"/>

    <div class="layui-form-item">
        <label class="layui-form-label">字典编码</label>
        <div class="layui-input-block">
            <input type="text" id="dcode" name="dcode" lay-verify="required" lay-reqtext="字典编码是必填项，岂能为空？" placeholder="请输入字典编码" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">字典名称</label>
        <div class="layui-input-block">
            <textarea placeholder="请输入字典名称" class="layui-textarea" id="dname" name="dname"></textarea>
        </div>
    </div>

    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">字典类型</label>
        <div class="layui-input-block">
            <textarea placeholder="请输入字典类型" class="layui-textarea" id="dtype" name="dtype"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit="demo1" lay-filter="demo1">立即提交</button>
        </div>
    </div>
</form>
<script>
    layui.use(['form', 'layedit', 'laydate'], function() {
        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate
            ,$=layui.jquery;

        //日期
        laydate.render({
            elem: '#date'
        });
        laydate.render({
            elem: '#date1'
        });

        //监听提交
        form.on('submit(demo1)', function(data){
            var id = "";
            $("input[name='did']:checked").each(function(index,items){
                id+=$(this).val()+",";
            });
            data.field.sportId=id;
            var formData = new FormData(document.getElementById("dicFrom"));
            var url = "../../dicAction/addAndUpdate";
            $.ajax({
                url:url,
                type:"post",
                data:formData,
                processData:false,
                contentType:false,
                dataType:"json",
                success:function(obj){
                    console.info(obj.state)
                    if(obj.num==1){
                        window.parent.location.reload();
                        var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                        parent.layer.close(index);
                    }else{
                        layer.msg(obj.msg);
                    }
                },
                error:function(e){
                    layer.msg("操作错误!");
                }
            });
            return false;
        });
        var id = getQueryString("id");
        if(id!=null){
            var url = "../../dicAction/findById";
            var data = {did:id};
            $.post(url,data,function(obj){
                $("#did").val(obj.did);
                $("#dcode").val(obj.dcode);
                $("#dname").val(obj.dname);
                $("#dtype").val(obj.dtype);
            },'json')

        }

    });
</script>

</body>
</html>